package com.ywm.msmService.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import com.ywm.msmService.service.MsmService;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Map;

@Service
public class MsmServiceImpl implements MsmService {
    //发送短信的方法
    @Override
    public Boolean sendMessage(Map<String, Object> param, String phoneNumber) {
        boolean empty = StringUtils.isEmpty(phoneNumber);
        if (empty) {
            return false;
        }
        //绑定阿里云
        DefaultProfile profile = DefaultProfile.getProfile("default", "LTAI4G6oa6Z3Cw9oRa8k1iZS", "eFQoPyUBHcN4G05oftLbEccbRrqcyk");
        IAcsClient client = new DefaultAcsClient(profile);
        //设置相关参数
        CommonRequest request = new CommonRequest();
        //固定参数
        request.setSysMethod(MethodType.POST);
        request.setSysDomain("dysmsapi.aliyuncs.com");
        request.setSysVersion("2017-05-25");
        request.setSysAction("SendSms");
        //设置发送相关参数
        request.putQueryParameter("PhoneNumbers", phoneNumber);//接收短信的手机号
        request.putQueryParameter("SignName", "个人学习制作在线教育网站");//阿里云短信签名的名称
        request.putQueryParameter("TemplateCode", "SMS_190729172");//阿里云模板名称(模板Code)
        request.putQueryParameter("TemplateParam", JSONObject.toJSONString(param));//验证码，转换成json数据
        //最终发送
        boolean b=false;
        try {
            CommonResponse response = client.getCommonResponse(request);
            b = response.getHttpResponse().isSuccess();
        } catch (ClientException e) {
            e.printStackTrace();
        }
        return b;
    }
}
